﻿using IRealize;
using Realize;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.Reflection;

namespace WebApplication9.Controllers.ClientSet
{
    // 职位分类设置
    public class ZWFLSZController : Controller
    {

        private readonly ILogger<TextController> _logger;
        private readonly IConfiguration _configuration;

        public ZWFLSZController(ILogger<TextController> logger, IConfiguration configuration)
        {
            _logger = logger;
            _configuration = configuration;
        }

        public IActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public async Task<IActionResult> GetTitles()
        {
            try
            {
                IConfig_Major_Kind config_Major_Kind = new Config_Major_Kind();
                List<Entity.Config_Major_Kind> zhi = await config_Major_Kind.selectCMK(_configuration["SqlServer"]);

                return Ok(new
                {
                    list = zhi
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"获取职称数据失败: {ex.Message}");
            }
        }

        [HttpGet]
        public async Task<IActionResult> Get(string id)
        {
            try
            {
                IConfig_Major_Kind config_Major_Kind = new Config_Major_Kind();
                var list = await config_Major_Kind.selectCMK(_configuration["SqlServer"]);
                var item = list.FirstOrDefault(x => x.mfk_id.ToString() == id);

                if (item == null)
                {
                    return NotFound("未找到该职位分类");
                }

                return Ok(new
                {
                    item = item
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"获取职称数据失败: {ex.Message}");
            }
        }

        [HttpDelete]
        public async Task<IActionResult> Delete(string id)
        {
            try
            {
                IConfig_Major_Kind config_Major_Kind = new Config_Major_Kind();
                int result = await config_Major_Kind.deleteCMK(id, _configuration["SqlServer"]);
                return Ok(new
                {
                    zhi = result
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"删除失败: {ex.Message}");
            }
        }

        [HttpGet]
        public IActionResult Add()
        {
            return View();
        }

        public IActionResult Fan()//返回
        {
            return RedirectToAction("Index");
        }

        [HttpPost]
        public async Task<IActionResult> Add([FromBody] NameModel model)
        {
            try
            {

                if (model == null || string.IsNullOrEmpty(model.Name))
                {
                    return BadRequest("职位分类名称不能为空");
                }

                IConfig_Major_Kind config_Major_Kind = new Config_Major_Kind();
                int result = await config_Major_Kind.insertCMK(model.Name, _configuration["SqlServer"]);

                if (result > 0)
                {
                    return Ok(new
                    {
                        zhi = result
                    });
                }
                else
                {
                    return StatusCode(500, "添加失败，数据库操作未成功");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "添加职位分类出错");
                return StatusCode(500, $"添加失败: {ex.Message}");
            }
        }

        [HttpGet]
        public IActionResult Upd()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Upd(IFormCollection form)
        {
            try
            {
                var id = form["id"].ToString();
                var name = form["name"].ToString();

                if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(name))
                {
                    return BadRequest("职位分类ID和名称不能为空");
                }

                IConfig_Major_Kind config_Major_Kind = new Config_Major_Kind();
                int result = await config_Major_Kind.updateCMK(id, name, _configuration["SqlServer"]);

                if (result > 0)
                {
                    return Ok(new
                    {
                        zhi = result
                    });
                }
                else
                {
                    return StatusCode(500, "修改失败，数据库操作未成功");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "修改职位分类出错");
                return StatusCode(500, $"修改失败: {ex.Message}");
            }
        }

        // 添加这个模型类
        public class NameModel
        {
            public string Name { get; set; }
        }
    }
}